package com.byt.framlib.commonutils;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.view.View;

import com.byt.framlib.R;
import com.byt.framlib.baseapp.BaseApplication;
import com.byt.framlib.commonwidget.selectordialog.dialog.DialogOnClickListener;
import com.byt.framlib.commonwidget.selectordialog.dialog.NormalAlertDialog;

public class InstallAppUtils {

    public static final int INP_REQUEST_CODE_INSTALL_APP = 996;

    public static void applyInstall(Activity context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (!BaseApplication.getAppContext().getPackageManager().canRequestPackageInstalls()) {
                NormalAlertDialog normalAlertDialog = new NormalAlertDialog.Builder(context)
                        .setButtonTextSize(14)
                        .setTitleVisible(true)
                        .setTitleText("温馨提示")
                        .setTitleTextSize(16)
                        .setContentText("请前往设置中开启安装应用所需的权限?")
                        .setContentTextSize(14)
                        .setContentTextColor(R.color.color_272727)
                        .setOnclickListener(new DialogOnClickListener() {
                            @Override
                            public void clickLeftButton(View view) {
                                context.finish();
                            }

                            @Override
                            public void clickRightButton(View view) {
                                Uri packageUri = Uri.parse("package:" + context.getPackageName());
                                Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageUri);
                                context.startActivityForResult(intent, INP_REQUEST_CODE_INSTALL_APP);
                            }
                        }).build();
                normalAlertDialog.show();
            }
        }
    }
}
